//
//  KSInboundDiffDetailViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/28.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit
import MJRefresh

/// 差异详情
class KSInboundDiffDetailViewController: KSBaseViewController {
    
    var orderNumber: String = ""
    @IBOutlet weak var tableView: UITableView!
    private var detailModel: KSInboundDiffDetailDataModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.title = "差异详情"
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.backgroundColor = UIColor.colorWithHexString(hex: "#f3f6f7")
        
        self.requestData()
    }
    
    private func requestData() {
        self.startLoading()
        self.request.requestStorageOrderDiffDetail(orderNumber: orderNumber)
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        if apiType == KSApiType.KSStorageOrderDiffDetail {
            let reusltModel = KSInboundDiffDetailModel.deserialize(from: result)
            self.detailModel = (reusltModel?.data)!
            if self.detailModel?.diff_order_goods?.count == 0 {
                
            } else {
                tableView.reloadData()
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension KSInboundDiffDetailViewController: UITableViewDelegate, UITableViewDataSource {
    
    // UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        var num = 0
        if self.detailModel != nil {
            num = (self.detailModel?.diff_order_goods?.count)!
        }
        return num
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    // UITableViewDelegate
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = KSInboundDiffDetailTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "KSInboundDiffDetailTableViewCell" + String.init(indexPath.section))
        cell.initView(model: (detailModel?.diff_order_goods![indexPath.section])!)
        return cell
    }
}
